package com.md24housecall.fibromyalgiadiagnostic;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class MainActivity extends FragmentActivity {
	private int questionOneScoreTotal = 0;
	private int questionTwoScoreTotal = 0;
	private int questionThreeScoreTotal = 0;
	
	boolean [] questionThreeCheckedCheckBoxs = new boolean [50];
	boolean [] questionOneCheckedCheckBoxs = new boolean [20];
	int[] questionTwoCheckedRadios = new int[3];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            IntroductionFragment firstFragment = new IntroductionFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }

	}
	
	public void onQuestionOneCheckboxClicked(View v){
    	CheckBox checkBox = (CheckBox) v;
    	LinearLayout layout = (LinearLayout)v.getParent();
    	int index = layout.indexOfChild(v);
    	if(checkBox.isChecked()){
    		questionOneScoreTotal += 1;
    		questionOneCheckedCheckBoxs[index] = true;
    		checkBox.setTypeface(null, Typeface.BOLD);
    		checkBox.setTextColor(getResources().getColor(R.color.checked_text));
    		checkBox.setShadowLayer(2, 2, 2, R.color.shadow_text);
        }else{
        	questionOneScoreTotal -= 1;
        	questionOneCheckedCheckBoxs[index] = false;
        	checkBox.setTypeface(null, Typeface.NORMAL);
        	checkBox.setTextColor(getResources().getColor(R.color.unchecked_text));
        	checkBox.setShadowLayer(0, 0, 0, R.color.shadow_text);
        }
    	
    }

	public void onQuestionTwoRadioClicked(View v){
		RadioButton clickedRadioButton = (RadioButton) v;
		RadioGroup selectedRadioGroup = (RadioGroup)v.getParent();
		for(int i = 0; i < selectedRadioGroup.getChildCount(); i++){
			RadioButton radioButton = (RadioButton) selectedRadioGroup.getChildAt(i);
			radioButton.setTypeface(null, Typeface.NORMAL);
			radioButton.setTextColor(getResources().getColor(R.color.unchecked_text));
			radioButton.setShadowLayer(0, 0, 0, R.color.shadow_text);
		}
		
		clickedRadioButton.setTypeface(null, Typeface.BOLD);
		clickedRadioButton.setTextColor(getResources().getColor(R.color.checked_text));
		clickedRadioButton.setShadowLayer(2, 2, 2, R.color.shadow_text);
		
		RadioGroup radioFatigueGroup = (RadioGroup) findViewById(R.id.fatigue_group_radio);
		RadioGroup radioWakingGroup = (RadioGroup) findViewById(R.id.waking_group_radio);
		RadioGroup radioCognitiveGroup = (RadioGroup) findViewById(R.id.cognitive_group_radio);
		
		int selectedFatigueId = radioFatigueGroup.getCheckedRadioButtonId();
		int selectedWakingId = radioWakingGroup.getCheckedRadioButtonId();
		int selectedCognitiveId = radioCognitiveGroup.getCheckedRadioButtonId();
		
		questionTwoCheckedRadios[0] = selectedFatigueId;
		questionTwoCheckedRadios[1] = selectedWakingId;
		questionTwoCheckedRadios[2] = selectedCognitiveId;
		
		RadioButton selectedFatigue = (RadioButton) findViewById(selectedFatigueId);
		RadioButton selectedWaking = (RadioButton) findViewById(selectedWakingId);
		RadioButton selectedCognitive = (RadioButton) findViewById(selectedCognitiveId);
		
		int fatigueIndex = radioFatigueGroup.indexOfChild(selectedFatigue);
		int wakingIndex = radioWakingGroup.indexOfChild(selectedWaking);
		int cognitiveIndex = radioCognitiveGroup.indexOfChild(selectedCognitive);
		
		
		questionTwoScoreTotal = fatigueIndex + wakingIndex + cognitiveIndex;

	}
	
	public int getQuestionOneScore(){
		return questionOneScoreTotal;
	}
	
	public int getQuestionTwoScore(){
		return questionTwoScoreTotal;
	}
	
	public int getQuestionThreeScore(){
		return questionThreeScoreTotal;
	}
	
	public void setQuestionThreeScore(int sc){
		questionThreeScoreTotal = sc;
	}
	
	public boolean[] getQuestionThreeCheckedCheckBoxs(){
		return questionThreeCheckedCheckBoxs;
	}
	
	public boolean[] getQuestionOneCheckedCheckBoxs(){
		return questionOneCheckedCheckBoxs;
	}
	
	public int[] getQuestionTwoCheckedRadios(){
		return questionTwoCheckedRadios;
	}

}
